package cn.zjitc.controller;

import cn.zjitc.entity.Result;
import cn.zjitc.pojo.User;
import cn.zjitc.service.LoginService;
import cn.zjitc.util.VertifyCodeUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

@RestController
@RequestMapping("/login")
public class LoginController {
    private String validate_code = null;
    @Reference
    private LoginService loginService;
    @RequestMapping("/login")
    public Result findPage(@RequestBody User users, HttpSession session){
        String code = users.getCode();
        if(code.equalsIgnoreCase(validate_code)){
            try{
                String username = users.getUser_name();
                String password = users.getUser_password();
                User user = loginService.login(username,password);
                if(user != null){
                    session.setAttribute("user",user);
                    return new Result(true,"登录成功",user);
                }else{
                    return new Result(false,"用户名或密码错误");
                }
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,"用户名或密码错误");
            }
        }else{
            return new Result(false,"验证码错误");
        }
    }
    @RequestMapping("/regist")
    public Result regist(@RequestBody User users,HttpSession session){
        String code = users.getCode();
        if(code.equalsIgnoreCase(validate_code)){
            try{
                String user_name =users.getUser_name();
                String user_password = users.getUser_password();
                User user = loginService.findByUserName(user_name);
                if(user == null){
                    loginService.regist(user_name,user_password);
                }else{
                    return new Result(false,"用户名重复");
                }
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,"用户名重复");
            }
            return new Result(true,"注册成功");
        }else{
            return new Result(false,"验证码错误");
        }


    }
    //生成验证码
    @GetMapping("/getVertifyCodeImage")
    public String getImageCode(HttpServletRequest request) throws IOException {
        // 1. 使用工具类生成验证码
        String code = VertifyCodeUtils.generateVerifyCode(4);
        validate_code = code; // 存放生成的验证码
        // 2. 将验证码放入ServletContext作用域
        request.getServletContext().setAttribute("code", code);
        // 3. 将图片转换成base64格式
        // 字节数组输出流在内存中创建一个字节数组缓冲区，所有发送到输出流的数据保存在该字节数组缓冲区中
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //将得到的验证码，使用工具类生成验证码图片，并放入到字节数组缓存区
        VertifyCodeUtils.outputImage(220,40,byteArrayOutputStream,code);
        //使用spring提供的工具类，将字节缓存数组中的验证码图片流转换成Base64的形式
        //并返回给浏览器
        return "data:image/png;base64," + Base64Utils.encodeToString(byteArrayOutputStream.toByteArray());
    }

}
